﻿namespace Hidistro.UI.Web.Admin.AliOH
{
    using ASPNET.WebControls;
    using Hidistro.ControlPanel.Store;
    using Hidistro.Entities;
    using Hidistro.Entities.VShop;
    using Hidistro.Membership.Context;
    using Hidistro.UI.ControlPanel.utility;
    using Hidistro.UI.Web.App_Code;
    using Hishop.Alipay.OpenHome;
    using Hishop.Alipay.OpenHome.AlipayOHException;
    using Hishop.Alipay.OpenHome.Model;
    using Hishop.Alipay.OpenHome.Request;
    using Hishop.Weixin.MP.Domain.Menu;
    using System;
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Hishop.Alipay.OpenHome.Response;

    public class ManageMenu : AdminPage
    {
        protected System.Web.UI.WebControls.Button btnSubmit;
        protected Grid grdMenu;

        private void BindData(ClientType clientType)
        {
            this.grdMenu.DataSource = VShopHelper.GetMenus(clientType);
            this.grdMenu.DataBind();
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(SettingsManager.GetMasterSettings(false).AliOHAppId))
            {
                base.Response.Write("<script>alert('您的服务号配置存在问题，请您先检查配置！');location.href='AliOHServerConfig.aspx'</script>");
            }
            else
            {
                IList<MenuInfo> initMenus = VShopHelper.GetInitMenus(ClientType.AliOH);
                List<Hishop.Alipay.OpenHome.Model.Button> list2 = new List<Hishop.Alipay.OpenHome.Model.Button>();
                foreach (MenuInfo info in initMenus)
                {
                    if ((info.Chilren == null) || (info.Chilren.Count == 0))
                    {
                        Hishop.Alipay.OpenHome.Model.Button item = new Hishop.Alipay.OpenHome.Model.Button {
                            name = info.Name,
                            actionParam = string.IsNullOrEmpty(info.Url) ? "http://javasript:;" : info.Url,
                            actionType = info.Type
                        };
                        list2.Add(item);
                    }
                    else
                    {
                        Hishop.Alipay.OpenHome.Model.Button button2 = new Hishop.Alipay.OpenHome.Model.Button {
                            name = info.Name
                        };
                        List<Hishop.Alipay.OpenHome.Model.Button> list3 = new List<Hishop.Alipay.OpenHome.Model.Button>();
                        foreach (MenuInfo info2 in info.Chilren)
                        {
                            Hishop.Alipay.OpenHome.Model.Button button3 = new Hishop.Alipay.OpenHome.Model.Button {
                                name = info2.Name,
                                actionParam = string.IsNullOrEmpty(info2.Url) ? "http://javasript:;" : info2.Url,
                                actionType = info2.Type
                            };
                            list3.Add(button3);
                        }
                        button2.subButton = list3;
                        list2.Add(button2);
                    }
                }
                Hishop.Alipay.OpenHome.Model.Menu menu = new Hishop.Alipay.OpenHome.Model.Menu {
                    button = list2
                };
                AlipayOHClient client = AliOHClientHelper.Instance(base.Server.MapPath("~/"));
                bool flag = false;
                try
                {
                    AddMenuRequest request = new AddMenuRequest(menu);
                    client.Execute<MenuAddResponse>(request);
                    this.ShowMsg("保存到服务窗成功！", true);
                    flag = true;
                }
                catch (AliResponseException)
                {
                }
                catch (Exception exception)
                {
                    this.ShowMsg("保存到服务窗失败，失败原因：" + exception.Message, false);
                    flag = true;
                }
                if (!flag)
                {
                    try
                    {
                        UpdateMenuRequest request2 = new UpdateMenuRequest(menu);
                        client.Execute<MenuUpdateResponse>(request2);
                        this.ShowMsg("保存到服务窗成功！", true);
                    }
                    catch (Exception exception2)
                    {
                        this.ShowMsg("保存到服务窗失败，失败原因：" + exception2.Message, false);
                    }
                }
            }
        }

        private SingleButton BuildMenu(MenuInfo menu)
        {
            switch (menu.BindType)
            {
                case BindType.Key:
                    return new SingleClickButton { name = menu.Name, key = menu.MenuId.ToString() };

                case BindType.Topic:
                case BindType.HomePage:
                case BindType.ProductCategory:
                case BindType.ShoppingCar:
                case BindType.OrderCenter:
                case BindType.MemberCard:
                case BindType.Url:
                    return new SingleViewButton { name = menu.Name, url = menu.Url };
            }
            return new SingleClickButton { name = menu.Name, key = "None" };
        }

        private void grdMenu_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int rowIndex = ((GridViewRow) ((Control) e.CommandSource).NamingContainer).RowIndex;
            int menuId = (int) this.grdMenu.DataKeys[rowIndex].Value;
            if (e.CommandName == "Fall")
            {
                VShopHelper.SwapMenuSequence(menuId, false);
            }
            else if (e.CommandName == "Rise")
            {
                VShopHelper.SwapMenuSequence(menuId, true);
            }
            else if (e.CommandName == "DeleteMenu")
            {
                if (VShopHelper.DeleteMenu(menuId))
                {
                    this.ShowMsg("成功删除了指定的分类", true);
                }
                else
                {
                    this.ShowMsg("分类删除失败，未知错误", false);
                }
            }
            this.BindData(ClientType.AliOH);
        }

        private void grdMenu_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int num = (int) DataBinder.Eval(e.Row.DataItem, "ParentMenuId");
                string str = DataBinder.Eval(e.Row.DataItem, "Name").ToString();
                if (num == 0)
                {
                    str = "<b>" + str + "</b>";
                }
                if (num > 0)
                {
                    str = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + str;
                }
                Literal literal = e.Row.FindControl("lblCategoryName") as Literal;
                literal.Text = str;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.grdMenu.RowCommand += new GridViewCommandEventHandler(this.grdMenu_RowCommand);
            this.grdMenu.RowDataBound += new GridViewRowEventHandler(this.grdMenu_RowDataBound);
            this.btnSubmit.Click += new EventHandler(this.btnSubmit_Click);
            if (!this.Page.IsPostBack)
            {
                this.BindData(ClientType.AliOH);
            }
        }

        protected string RenderInfo(object menuIdObj)
        {
            ReplyInfo reply = ReplyHelper.GetReply((int) menuIdObj);
            if (reply != null)
            {
                return reply.Keys;
            }
            return string.Empty;
        }
    }
}

